package homework.tools;

import java.util.function.Consumer;
import java.util.function.Function;

public class FunctionalTrait {
	
	public static <T> T identity(final T t) { return t; }
	
	public static <T, R> R decorate(final T that, 
			final Function<T, R> function) {
		return function.apply(that);
	}
	
	public static <T, R> R either(final T that, 
			final Function<T, Boolean> condition, 
			final Function<T, R> trueSupplier, 
			final Function<T, R> falseSupplier) {
		if(condition.apply(that)) return trueSupplier.apply(that); else return falseSupplier.apply(that);
	}
	
	public static <T> void letWhen(final T that, 
			final Function<T, Boolean> condition, 
			final Consumer<T> consumer) {
		if(condition.apply(that)) consumer.accept(that);
	}
	public static <T> void letIf(final T that, 
			final Function<T, Boolean> condition, 
			final Consumer<T> trueAction, 
			final Consumer<T> falseAction) {
        if(condition.apply(that)) trueAction.accept(that); else falseAction.accept(that);
    }	
	public static <T> void let(final T that, 
			final Consumer<T> consumer) {
		consumer.accept(that);
	}	
}
